import 'dart:async';

import 'package:flutter/services.dart';

enum PaypalEnvironment { live, sandbox, stage }

class PaypalFlutter {
  static const MethodChannel _channel = const MethodChannel('paypal_flutter');

  static Future<String?> get platformVersion async {
    final String? version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }

  ///
  /// Chiamata di configurazione dell'SDK.
  ///
  /// Risposta:
  ///  - OK -> configurato correttamente
  ///  - Errore: -> risposta di errore dall'SDK
  ///
  static Future<String?> configSetup(String returnURL, String clientID,
      PaypalEnvironment paypalEnvironment) async {
    try {
      String? response = await _channel.invokeMethod("setup", {
        "returnUrl": returnURL,
        "clientID": clientID,
        "environment": environmentDecoding(paypalEnvironment)
      });
      return response;
    } catch (error) {
      print("Error: $error");
      return null;
    }
  }

  ///
  /// Chiamata al pagamento
  /// Il totale deve essere una stringa nel formato "13.50" -> 13 euro e 50 cent
  ///
  static Future<dynamic> pay(String amount) async {
    try {
      var response = await _channel.invokeMethod("pay", {"total": amount});
      return response;
    } catch (error) {
      print("Error: $error");
      return null;
    }
  }

  static int environmentDecoding(PaypalEnvironment _paypalEnvironment) {
    switch (_paypalEnvironment) {
      case PaypalEnvironment.live:
        return 0;
      case PaypalEnvironment.sandbox:
        return 1;
      case PaypalEnvironment.stage:
        return 2;
    }
  }
}
